<?php
namespace Admin\Controller;
use  Think\Controller;
use Think\Exception;
use Think\Page;
use Think\Think;

/**
 * Class MenuController
 * @package Admin\Controller
 */
class MenuController extends CommonController{

    /**
     * 新增模块
     */
    public function add(){
        if (IS_POST){
            if (!isset($_POST['name']) || !$_POST['name']){
                return show(0, "菜单名不能为空");
            }
            if (!isset($_POST['m']) || !$_POST['m']){
                return show(0, "模块名不能为空");
            }
            if (!isset($_POST['c']) || !$_POST['c']){
                return show(0, "控制器不能为空");
            }
            if (!isset($_POST['f']) || !$_POST['f']){
                return show(0, "方法名不能为空");
            }
            if (I('menu_id')){
                return $this->save(I('post.'));
            }
            $menuId = D('Menu')->insert($_POST); // 这里model类中继承父类的__construct 后就可以使用add方法了
            if ($menuId){
                return show(1, "新增成功");
            }
            return show(0, '新增失败');
        }
        $this->display();
    }

    /**
     * 菜单列表
     */
    public function index(){
        $map = [];
        if (isset($_REQUEST['type']) && in_array($_REQUEST['type'] , [0 , 1])){
            $map['type'] = intval($_REQUEST['type']);
            $this->assign('type' , $_REQUEST['type']);
        }else{
            $this->assign('type' , -1);
        }
        $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
        $pageSize = $_REQUEST['pagesize'] ? $_REQUEST['pagesize'] : 3 ;
        $menus = D("Menu")->getMenus($map , $page , $pageSize);
        $menusCount = D("Menu")->getMenuCount($map);
        $res = new \Think\Page($menusCount , $pageSize);
        $pageRes = $res->show();
        $this->assign('pageRes' , $pageRes);
        $this->assign('menus' , $menus);
        $this->display();
    }

    /**
     * 菜单编辑
     */
    public function edit(){
        $id = I('id');
        $res = D('Menu')->find($id);
        $this->assign('res' , $res);
        $this->display();
    }

    /**
     * @param $data
     */
    public function save($data){
        $menuId = $data['menu_id'];
        unset($data['menu_id']);
        try{
            $id = D('Menu')->updataMenuById($menuId , $data);
            if($id === false){
                return show(0,'更新失败');
            }
            return show(1,'更新成功');
        }catch (Exception $e){
            return show(0,$e->getMessage());
        }
    }

    /**
     *执行删除操作
     */
    public function setstatus(){
        try{
            if($_POST){
                $id = $_POST['id'];
                $status = $_POST['status'];
                //执行更新操作【方法一】
//                $res = D('Menu')->updateStatusById($id , $status);
                //执行更新操作[方法二]
                $data['status'] = $status;
                $res = D('Menu')->where('menu_id=' . $id)->save($data);
                if($res){
                    return show(1,'操作成功');
                }else{
                    return show(0,'操作失败');
                }
            }
        }catch(Exception $e){
            return show(0,$e->getMessage());
        }
        return show(0,'没有提交数据');
    }

    /**
     * 跟新排序操作
     */
    public function listorder()
    {
        $listorder = $_POST['listorder'];
        $jump_url = $_SERVER['HTTP_REFERER'];
        $errors = [];
        if ($listorder) {
            try{
                foreach ($listorder as $menuId => $v) {
                    //执行数据排序操作
                    $id = D('Menu')->updateListorderById($menuId, $v);
                    if ($id === false) {
                        $errors[] = $menuId;
                    }
                }
            }catch (Exception $e){
                return show(0, $e->getMessage(), ['jump_url' => $jump_url]);
            }
            if ($errors) {
                return show(0, '排序失败-' . implode(',', $errors), ['jump_url' => $jump_url]);
            }

            return show(1, '排序成功' ,['jump_url'=>$jump_url]);
        }
        return show(0, '排序数据失败', ['jump_url' => $jump_url]);
    }

}